package com.hd123.rumba.jsp;

import java.util.Calendar;
import java.util.Date;

import com.hd123.rumba.util.StringUtil;
import com.hd123.rumba.util.lunar.LunarDate;

public class WeeklyCalendar {
  /**
   * 返回按周次的日历的html table
   * @param startDate
   * @param endDate
   * @return
   */
  public static String weekCalendar(Date startDate, Date endDate) {
    StringBuffer sb = new StringBuffer();
    Date today = StringUtil.truncateToDate(new Date());
    startDate = StringUtil.truncateToDate(startDate);
    endDate = StringUtil.truncateToDate(endDate);
    String cellStyle = "background-color:#eeeeee;text-align:center;";
    String cell = "<td style=\"width:40px;" + cellStyle + "\">";
    String sundayStyle = "background-color:#eeeeee;text-align:center;color:red;";
    String sundayTd = "<td style=\"width:40px;" + sundayStyle + "\">";
    String saturdayStyle = "background-color:#eeeeee;text-align:center;color:blue;";
    String saturdayTd = "<td style=\"width:40px;" + saturdayStyle + "\">";
    String firstStyle = "background-color:blue;text-align:center;color:white;";
    // String firstTd = "<td style=\"width:40px;" + firstStyle + "\">";
    String todayStyle = "background-color:yellow;text-align:center;color:blue;";
    // String todayTd = "<td style=\"width:40px;" + todayStyle + "\">";
    sb.append("<table style=\"font-size:9pt;font-family:verdana\">");
    sb.append("<tr>");
    sb.append(cell + "周</td>");
    sb.append(sundayTd + "日</td>");
    sb.append(cell + "一</td>");
    sb.append(cell + "二</td>");
    sb.append(cell + "三</td>");
    sb.append(cell + "四</td>");
    sb.append(cell + "五</td>");
    sb.append(saturdayTd + "六</td>");
    sb.append("</tr>");
    int week = 0;
    Calendar c = Calendar.getInstance();
    c.setTime(startDate);
    LunarDate ld = new LunarDate();
    int dow = c.get(Calendar.DAY_OF_WEEK);
    if (dow > 1) {
      sb.append("<tr>");
      sb.append(cell + (++week) + "</td>");
      for (int i = 1; i < dow; ++i)
        sb.append(cell + "&nbsp</td>");
    }
    while (!c.getTime().after(endDate)) {
      dow = c.get(Calendar.DAY_OF_WEEK);
      int dom = c.get(Calendar.DAY_OF_MONTH);
      if (dow == 1) {
        sb.append("<tr>");
        sb.append(cell + (++week) + "</td>");
      }

      String tdStyle = cellStyle;
      if (c.getTime().equals(today))
        tdStyle = todayStyle;
      else if (dom == 1)
        tdStyle = firstStyle;
      else if (dow == 1)
        tdStyle = sundayStyle;
      else if (dow == 7)
        tdStyle = saturdayStyle;
      sb.append("<td style=\"width:40px;" + tdStyle + "\">");

      String sds = Integer.toString(dom);
      if (dom == 1 || c.getTime().equals(startDate))
        sds = c.get(Calendar.MONTH) + 1 + "月";
      ld.setDate(c.getTime());
      String lds = ld.getDateString();
      if ("初一".equals(lds))
        lds = ld.getMonthString();
      if ("正月".equals(ld.getMonthString()))
        lds = ld.getChineseYearString();
      sb.append("<table width=100% style=\"font-size:9pt;font-family:verdana\">");
      sb.append("<tr>");
      sb.append("<td nowrap style=\"" + tdStyle + "\">");
      sb.append(sds);
      sb.append("</td>");
      sb.append("<td align=right width=1 style=\"" + tdStyle.replace("text-align:center", "")
          + "\">");
      sb.append(lds);
      sb.append("</td></tr></table>");

      sb.append("</td>");

      if (dow == 7)
        sb.append("</tr>");
      c.add(Calendar.DATE, 1);
    }
    sb.append("</table>");
    return sb.toString();
  }

}
